package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.service.PayService;
import com.atguigu.cloud.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;

    @Operation(summary = "新增",description = "增加支付流水")
    @PostMapping("pay/add")
    public Result<String> addPay(@RequestBody Pay pay) {
        payService.add(pay);
        return Result.success("success");
    }

    @Operation(summary = "删除",description = "删除支付流水")
    @DeleteMapping("pay/delete/{id}")
    public Result<Integer> deletePay(@PathVariable("id") Integer id) {
        Integer row = payService.delete(id);
        return Result.success(row);
    }

    @Operation(summary = "更新",description = "更新支付流水")
    @PutMapping("pay/update")
    public Result<Integer> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        Integer row = payService.update(pay);
        return Result.success(row);
    }

    @Operation(summary = "根据id查询",description = "根据id查询支付流水")
    @GetMapping("pay/get/{id}")
    public Result<Pay> getPay(@PathVariable("id") Integer id) {
        Pay pay = payService.get(id);
        return Result.success(pay);
    }

    @Operation(summary = "查询所有",description = "查询所有支付流水")
    @GetMapping("pay/getAll")
    public Result<List<Pay>> getAllPay() {
        List<Pay> all = payService.getAll();
        return Result.success(all);
    }

    @Value("${server.port}")
    private String port;
    @GetMapping("test")
    public String test(@Value("${cloud.name}") String name){
        return port+name;
    }

}
